<template>
  <section class="f-commission">
    <div class="w-card my-content-box">
      <div class="title-box">
        <div>
          <span>佣金累计发放总额（元）</span>
        </div>
        <!-- <div>
          <el-button>配置拉新策略</el-button>
          <el-button type="primary" plain>配置提现规则</el-button>
        </div> -->
      </div>
      <div class="title-text" style="font-size:24px;padding:5px 0 12px 0">{{incomeAll.totalIncomeTotal}}</div>

      <section class="f-commission-box">
        <div style="background: #FFE5E5;">
          <p>待入账（元）</p>
          <p class="f-price">{{incomeAll.totalIncomeFreezen}}</p>
        </div>
        <div style="background: #E5F2FF;">
          <p>已发放（元）</p>
          <p class="f-price">{{incomeAll.totalIncomeRest}}</p>
        </div>
        <div style="background: #BCFFD8;">
          <p>可提现（元）</p>
          <p class="f-price">{{incomeAll.totalIncomeRestAvailable}}</p>
        </div>
        <div style="background: #FFE2BC;">
          <p>已提现（元）</p>
          <p class="f-price">{{incomeAll.totalIncomeWithdrawUsed}}</p>
        </div>
      </section>
    </div>

    <div class="w-card my-content-box" style="margin-top: 14px;">
      <div class="title-box">
        <span class="title-text">商户佣金列表</span>
      </div>
      <div class="my-search-box">
        <div>
          <span>商户：</span>
          <el-select v-model="searchForm.shopId" clearable>
            <el-option v-for="item in shopList" :key="item.shopId" :label="item.shopName" :value="item.shopId" />
          </el-select>
        </div>
        <!-- <div>
          <span>余额区间：</span>
          <el-radio-group v-model="searchForm.incomeQryType">
            <el-radio :label="1">待提现佣金</el-radio>
            <el-radio :label="2">累计佣金</el-radio>
          </el-radio-group>
        </div> -->
        <div style="width:400px">
          <span>待提现佣金：</span>
          <el-input v-model="searchForm.incomeMin" type="number" clearable placeholder="最低" />
          <div style="padding:0 10px">~</div>
          <el-input v-model="searchForm.incomeMax" type="number" clearable placeholder="最高" />
        </div>
        <div class="f-ml-20">
          <el-button @click="searchHandle" type="primary">查询</el-button>
          <el-button @click="resetHandle">重置</el-button>
        </div>
      </div>

      <div class="my-table-box">
        <el-table :height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
          :cell-style="{fontSize:'14px',color:'#7b7b7d'}" :data="tableData" stripe>
          <el-table-column type="index" width="55" label="序号" align="center" />
          <el-table-column prop="shopName" label="商户名称" align="center">
            <template v-slot="scope">
              {{ scope.row.shopName || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="incomeTotal" label="累计佣金总额（元）" align="center">
            <template v-slot="scope">
              {{ scope.row.incomeTotal || '' }}
              <!-- <el-link style="font-size:12px" type="primary" @click="pageJump('/orderIncome',scope.row)">
                {{ scope.row.incomeTotal || '' }}
              </el-link> -->
            </template>
          </el-table-column>
          <el-table-column prop="incomeFreezen" label="待入账（元）" align="center">
            <template v-slot="scope">
              {{ scope.row.incomeFreezen || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="incomeRest" label="已发放（元）" align="center">
            <template v-slot="scope">
              {{ scope.row.incomeRest || '' }}
              <!-- <el-link style="font-size:12px" type="primary" @click="pageJump('/orderIncome',scope.row,2)">
                {{ scope.row.incomeRest || '' }}
              </el-link> -->
            </template>
          </el-table-column>
          <el-table-column prop="incomeRestAvailable" label="可提现（元）" align="center">
            <template v-slot="scope">
              {{ scope.row.incomeRestAvailable || '' }}
              <!-- <el-link style="font-size:12px" type="primary" @click="pageJump('/orderWithdraw',scope.row)">
                {{ scope.row.incomerest || '' }}
              </el-link> -->
            </template>
          </el-table-column>
          <!-- <el-table-column prop="waitGrant" label="待发放（元）" align="center">
            <template v-slot="scope">
              <el-link style="font-size:12px" type="primary" @click="pageJump('/orderWithdraw',scope.row)">
                {{ scope.row.waitGrant || '' }}
              </el-link>
            </template>
          </el-table-column> -->
        </el-table>
        <div class="my-table-footer">
          <el-pagination style="margin-left:auto;" v-model:currentPage="page" v-model:page-size="size"
            :page-sizes="[10, 20, 50, 100]" small :background="true" layout="total, sizes, prev, pager, next, jumper"
            :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
      </div>
    </div>
  </section>
</template>

<script>
import { onMounted, reactive, ref, toRefs, watch } from 'vue'
import { getWindowWidthAndHeight, warningDeal, getDateTime, successDeal, BlobDownLoad, toThousands } from '@/utils/utils'
import { useRoute, useRouter } from 'vue-router'
import api from './api'

export default {
  setup() {
    const route = useRoute()
    const router = useRouter()

    const state = reactive({
      isDetail: false,

      searchForm: {
        "shopId": "",
      },

      tableHeight: 0,
      tableData: [],
      page: 1,
      size: 10,
      total: 0,

      shopList: [],
      incomeAll: {},
    })

    watch(() => route.path, (newVal, oldVal) => {
      setIsDetail()
    })

    function setIsDetail() {
      state.isDetail = (route.path != '/commission')
      getTableData()
    }

    function searchHandle() {
      state.page = 1
      getTableData()
    }

    function resetHandle() {
      state.searchForm = {}
      searchHandle()
    }

    function getTableData() {
      const obj = {
        page: state.page,
        size: state.size,
        ...state.searchForm,
        incomeRestAvailableMin: state.searchForm.date && state.searchForm.date[0],
        incomeRestAvailableMax: state.searchForm.date && state.searchForm.date[1],
        orderBy: 'id desc'
      }
      api.incomeShopAccount(obj).then(res => {
        if (res.code == '200') {
          state.tableData = res.data.list
          state.total = res.data.total
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function handleCurrentChange(e) {
      state.page = e
      getTableData()
    }

    function handleSizeChange(e) {
      state.size = e
      getTableData()
    }

    function lookDetail() {
      router.push({ path: '/orderRecharge/detail' })
    }

    function exportHandle() {
      const obj = {
        responseType: 'blob',
      }
      api.shopExportAll(obj).then(res => {
        const _fileName = decodeURIComponent(res.headers['content-disposition'].split(';')[1].split('=')[1])
        BlobDownLoad(res, _fileName)
      }).catch(err => {
        console.log(err)
      })
    }

    const getIncomeAll = () => {
      api.incomeStatistics().then(res => {
        state.incomeAll = res.data
      })
    }

    const getShopAll = () => {
      api.shopAll().then(res => {
        state.shopList = res.data
      })
    }

    const pageJump = (path, data, incomeState) => {
      return
      router.push({
        path,
        query: {
          shopName: data.shopName,
          incomeState: incomeState
        }
      })
    }

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 500
      setIsDetail()
      getIncomeAll()
      getShopAll()
    })

    return {
      ...toRefs(state),
      exportHandle,
      searchHandle,
      resetHandle,
      lookDetail,
      handleCurrentChange,
      handleSizeChange,
      getDateTime,
      toThousands,
      pageJump,
    }
  }
}
</script>

<style lang="scss" scoped>
.f-commission {
  .my-content-box {
    box-sizing: border-box;
    padding: 20px;
    .title-box {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .title-text {
        font-size: 16px;
        font-weight: bold;
      }
    }

    .f-commission-box {
      display: flex;
      align-items: center;
      justify-content: space-between;
      width: 100%;
      > div {
        width: 21%;
        padding: 13px 25px 7px 16px;
        font-weight: 300;
        font-size: 15px;
        color: #666666;
        .f-price {
          margin-top: 17px;
          color: #000000;
          text-align: right;
          font-size: 24px;
          font-weight: 400;
        }
      }
    }

    .my-search-box {
      box-sizing: border-box;
      padding: 20px 20px 20px 0;
      padding-bottom: 0;
      font-size: 14px;
      display: flex;
      align-items: center;
      gap: 10px 20px;

      > div {
        width: 300px;
        display: flex;
        align-items: center;

        > span {
          flex-shrink: 0;
          width: 90px;
          text-align: right;
        }
      }

      .datebox {
        width: 400px;
      }
    }

    .my-table-box {
      margin-top: 20px;

      .table-handle-box {
        display: flex;
        justify-content: space-around;
        align-items: center;
      }

      .my-table-footer {
        display: flex;
        align-items: center;
        margin-top: 10px;
        flex-wrap: wrap;
        gap: 10px;
      }
    }
  }
}
</style>